package com.zeng.stpro.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeng.stpro.commons.utils.Constant;
import com.zeng.stpro.modules.app.bean.SysMenu;
import com.zeng.stpro.modules.app.bean.SysUser;
import com.zeng.stpro.modules.app.mapper.SysMenuMapper;
import com.zeng.stpro.modules.app.mapper.SysUserMapper;
import com.zeng.stpro.modules.app.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SysMenuServiceImpl
 * @Description TODO
 * @Author zhijun zeng at 22:59 2021/2/3
 * @Version 1.0
 **/
@CacheConfig(cacheNames = "sys-menu")
@Transactional
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HashOperations hashOperations;

    @Cacheable(key = "#username")//  sys-menu::admin  作为 redis 的 key
    @Override
    public List<SysMenu> findUserMenuListByUsername(String username) {
        //SysUser sysUser = sysUserMapper.findByUsername(username);
        //SysUser sysUser = new SysUser();
        //if (Constant.UserType.SUPER_ADMIN.val().equalsIgnoreCase(sysUser.getUserType())) {//超级管理员

        if("admin".equalsIgnoreCase(username)){
            return getAllMenuList(null);
        }
        List<Long> menuIds = this.baseMapper.findUserMenuListByUsername(username);
        return getAllMenuList(menuIds);
    }


    private List<SysMenu> getAllMenuList(List<Long> menuIds) {
        //获取根菜单
        List<SysMenu> parentList = findParentMenuList(0l, menuIds);
        //获取子菜单
        getSubMenuList(parentList, menuIds);
        return parentList;
    }

    /**
     * 递归
     * @param parentList
     * @param menuIds
     * @return
     */
    private List<SysMenu> getSubMenuList(List<SysMenu> parentList, List<Long> menuIds) {
        List<SysMenu> subMenuList = new ArrayList<>();
        parentList.forEach(pm -> {
            if (pm.getMenuType() == Constant.MenuType.CATALOG.val()) {
                pm.setList(getSubMenuList(findParentMenuList(pm.getMenuId(), menuIds), menuIds));
            }
            subMenuList.add(pm);
        });
        return subMenuList;
    }

    private List<SysMenu> findParentMenuList(Long parentId, List<Long> menuIds) {
        List<SysMenu> pMenuList = findParentMenuList(parentId);
        if (menuIds == null) {
            return pMenuList;
        }
        List<SysMenu> userMenuList = new ArrayList<>();
        pMenuList.forEach(m -> {
            if (menuIds.contains(m.getMenuId())) {
                userMenuList.add(m);
            }
        });
        return userMenuList;
    }

    /**
     * 查询根目录
     * @param parentId
     * @return
     */
    private List<SysMenu> findParentMenuList(Long parentId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.nested(i -> i.eq("parent_id", parentId).eq("delete_status", "0"));
        List<SysMenu> pMenuList = this.baseMapper.selectList(queryWrapper);
        return pMenuList;
    }




}
